package com.patrol.logisticsmanage.ui.todo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.ui.appear.model.ServiceOrder;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ToDoAdapter extends RecyclerView.Adapter<ToDoAdapter.ViewHolder> {
    Context context;
    OnItemClickListener onItemClickListener;
    List<ServiceOrder> serviceOrders;

    public ToDoAdapter(Context context,List<ServiceOrder> serviceOrders, OnItemClickListener onItemClickListener) {
        this.context = context;
        this.onItemClickListener = onItemClickListener;
        this.serviceOrders = serviceOrders;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_todo, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onItemClick(v, position);
            }
        });
        ServiceOrder serviceOrder = serviceOrders.get(position);
        holder.tvTodoTime.setText(String.format(context.getString(R.string.todo_time),serviceOrder.shangmsd));
        holder.tvCreateTime.setText(String.format(context.getString(R.string.create_time),serviceOrder.createTime));
        holder.tvAssignTime.setText(String.format(context.getString(R.string.assign_time),null ==(serviceOrder.assignTime)?"":serviceOrder.assignTime));
        holder.tvFinishTime.setText(String.format(context.getString(R.string.finish_time),null ==(serviceOrder.finishTime)?"":serviceOrder.finishTime));
        holder.tvTodoAddress.setText(
                String.format(context.getString(R.string.todo_address),
                        serviceOrder.yingqId_dictText+" "+serviceOrder.louy_dictText+" "+serviceOrder.floor+"层"+serviceOrder.room));
        holder.tvTodoContent.setText(serviceOrder.problem);
        holder.tvTodoStatus.setText(serviceOrder.weixzt_dictText);
    }

    @Override
    public int getItemCount() {
        return serviceOrders.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_todo_time)
        TextView tvTodoTime;
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_assign_time)
        TextView tvAssignTime;
        @BindView(R.id.tv_finish_time)
        TextView tvFinishTime;
        @BindView(R.id.tv_todo_address)
        TextView tvTodoAddress;
        @BindView(R.id.tv_todo_content)
        TextView tvTodoContent;
        @BindView(R.id.tv_todo_status)
        TextView tvTodoStatus;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

}
